<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\AdjustmentAction;
use App\Admin\Grid\NGrid;
use App\Models\AmountModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Show;

class AmountController extends AdminController
{
    protected $title = '余额';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new AmountModel);
        $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'amount.user_id')->orderByDesc('id')
            ->select(['amount.*', 'user_mobile']);
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', 'ID')->integer();
                $filter->equal('shop_user-user_mobile', __('staff.user_mobile'))->integer();
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('type', __('l.type'))->select(__('amount.typeArr'));
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('user_mobile', __('l.user_mobile'));
        $grid->column('type', __('l.type'))->using(__('amount.typeArr'));
        $grid->column('user_type_id', __('l.user_type_id'))->using(__('l.userTypeIdArr'));
        $grid->column('title', __('l.title'));
        $grid->column('object_type', __('l.objectType'))->using(__('amount.objectTypeArr'));
        $grid->column('object_id', __('l.objectId'));
        $grid->column('amount', __('l.amount'));
        $grid->column('balance', __('l.balance'));
        $grid->column('status', __('amount.status'));
        $grid->column('finance_id', __('amount.finance_id'));
        $grid->column('created_at', __('l.createdAt'));
        $grid->disableActions(false);
        $grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableView(false);
            $action->disableEdit();
            $action->add(new AdjustmentAction);
        });
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(AmountModel::query()->leftJoin('shop_user', 'shop_user.user_id', 'amount.user_id')->find($id,
            ['amount.*', 'user_mobile']));
        $show->field('id', 'ID');
        $show->field('serial_number', __('l.serialNumber'));
        $show->field('user_mobile', __('l.user_mobile'));
        $show->field('user_type_id', __('l.user_type_id'))->using(__('l.userTypeIdArr'));
        $show->field('title', __('l.title'));
        $show->field('object_type', __('l.objectType'))->using(__('amount.objectTypeArr'));
        $show->field('object_id', __('l.objectId'));
        $show->field('amount', __('l.amount'));
        $show->field('balance', __('l.balance'));
        $show->field('status', __('amount.status'));
        $show->field('finance_id', __('amount.finance_id'));
        $show->field('description', __('amount.description'));
        $show->field('extend', __('l.extend'));
        $show->field('created_at', __('l.createdAt'));
        $show->field('updated_at', __('l.updatedAt'));
        return $show;
    }
}
